/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aqu.persistencia.utilidad;

import i18n.Constantes;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author VIJES
 */
public class Reportes {

    private Connection conn;
    private String user, password, url;
    public Reportes() {
        try{
            user = Constantes.USER_BDD;
            password = Constantes.PASSWORD_BDD;
            url = Constantes.URL;
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,user, password);
        }catch(ClassNotFoundException ex){
            ex.getMessage();
        }catch(SQLException ex){
            ex.getMessage();
        }
    }
    
    public void runReporte(String nomArchivo,Map parametro) throws JRException, Exception{
        try{
           
           //String master =  System.getProperty("user.dir") +  "/web/Reportes/PlanCuentas.jasper";
           //String master =  "/web/Reportes/PlanCuentas.jasper";
           String master =  Constantes.URL_REPORTES + nomArchivo + ".jasper";
           if(master == null){
               throw new Exception("Error no se encuentra el archivo del maestro Reporte");
           }
           JasperReport masterReport = null;
           File file = new File(master);
           masterReport = (JasperReport) JRLoader.loadObject(file);
           JasperPrint jasperPrint = JasperFillManager.fillReport(masterReport, parametro, conn);
           
           JRExporter exporter = new JRPdfExporter();
        
           exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
           exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new java.io.File("%temp%" + nomArchivo + ".pdf"));
           exporter.exportReport();
           
           //JasperViewer jviewer = new JasperViewer(jasperPrint, false);
           //jviewer.setVisible(true);
           
           Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+"%temp%" + nomArchivo + ".pdf");
           
        }catch (JRException ex) {
            System.out.println("Error: " + ex.getMessage());
            throw new JRException("Error en Reportes " + ex.getMessage());
        } 
        catch (Exception ex){
            System.out.println("Error: " + ex.getMessage());
            throw new Exception("ErrorGeneral: " + ex.getMessage());
        }
        finally{
            if(!conn.isClosed())
                conn.close();
        }
    }
    
}
